﻿@model RoadFlow.Model.ProgramRun
<form method="post">
    @Html.AntiForgeryToken()
    <!--工具栏按钮-->
    @if (!string.IsNullOrWhiteSpace(Model.Button_Toolbar))
    {
        <div class="toolbar" style="margin-top:0; border-top:none 0; position:fixed; top:0; left:0; right:0; margin-left:auto; z-index:999; width:100%; margin-right:auto;">
            @Html.Raw(Model.Button_Toolbar)
        </div>
        <div style="height: 35px;"></div>
    }
    <!--工具栏按钮-->
    <!--查询-->
    @if (!string.IsNullOrWhiteSpace(Model.QueryHtml))
    {
        <div style="height: 15px;"></div>
        <div>
            <table cellpadding="0" cellspacing="0" border="0" width="100%">
                <tr>
                    <td>
                        @Html.Raw(Model.QueryHtml)
                        @if (1 == Model.ButtonLocation)
                        {
                            @Html.Raw(Model.Button_Normal)
                        }
                    </td>
                </tr>
                @if (0 == Model.ButtonLocation)
                {
                    <tr>
                        <td style="text-align: center; padding: 10px 0;">@Html.Raw(Model.Button_Normal)</td>
                    </tr>
                }
            </table>
            @if (1 == Model.ButtonLocation)
            {
                <div style="height: 15px;"></div>
            }
        </div>
    }
    <!--查询-->
    <!--列表-->
    <table id="listtable"></table>
    @if (1 == Model.IsPager)
    {
        <div class="buttondiv"></div>
    }
    <!--列表-->
</form>
<script type="text/javascript">
    var curPageSize = "@ViewData["pagesize"]";
    var curPageNumber = "@ViewData["pagenumber"]";
    var isPager = "1" == "@Model.IsPager";//是否分页
    var gridHeight;//grid高度
    if (!isPager) {
        gridHeight = $(window).height()-30;
        if ("1" == "@ViewData["isToolbar"]") {
            gridHeight -= 50;
        }
        if ("1" == "@ViewData["isQuery"]") {
            gridHeight -= 50;
        }
        if ("1" == "@ViewData["isQueryNewtr"]") {
            gridHeight -= 30;
        }
    }
    else {
        gridHeight = "100%"
    }
    $(function () {
        $("#listtable").jqGrid({
            url: "Run_Query?@Html.Raw(ViewData["query"])",
            mtype: 'POST',
            postData:{pagesize:curPageSize, pagenumber:curPageNumber || 1},
            datatype: "json",
            colNames: @Html.Raw(Model.GridColNames),
            colModel: @Html.Raw(Model.GridColModels),
            height: gridHeight,
            multiselect: "0"!="@Model.SelectColumn", //是否添加选择列
            multiboxonly: "1"=="@Model.SelectColumn", //只能选择一行，单选
            gridComplete: hideSelectAll,//隐藏全选按钮
            beforeSelectRow: "1" == "@Model.SelectColumn" ? beforeSelectRow : null,
            rownumbers: "1" == "@Model.RowNumber",//序号列
            width: $(window).width(),
            sortname: "@Model.DefaultSort",
            sortorder: "DESC",
            loadComplete: function () {
                var gridObj = $("#listtable");
                var records = gridObj.getGridParam("userData");
                curPageSize = records.pagesize;
                curPageNumber = records.pagenumber;
                if (isPager) {
                    $(".buttondiv").html(RoadUI.Core.getPager1(records.total, records.pagesize, records.pagenumber, "query"));
                }
            }
        });
    });
    $(window).resize(function () {
        $("#listtable").setGridWidth($(window).width());
    });
    //查询
    function query(size, number) {
        var data = @Html.Raw(Model.QueryData);
        $("#listtable").setGridParam({postData: data}).trigger("reloadGrid");
    }
    //只能选择一行
    function beforeSelectRow() {
        $("#listtable").jqGrid('resetSelection');
        return (true);
    }
    //隐藏全选按钮
    function hideSelectAll() {
        if ("1" != "@Model.SelectColumn") {
            return;
        }
        $("#cb_listtable").hide();
        return (true);
    }
    //得到选择行的ID
    function getSelectIds() {
        var rowIds = $("#listtable").jqGrid('getGridParam', 'selarrrow');
        return rowIds;
    }
    //新增编辑数据
    function edit(id, dilplay) {
        var edit_url = "@ViewData["edit_url"]";
        var edit_model = "@ViewData["edit_model"]";
        var edit_width = "@ViewData["edit_width"]";
        var edit_height = "@ViewData["edit_height"]";
        var edit_title = "@ViewData["edit_title"]";
        var tabId = "@ViewData["tabid"]";
        var backUrl = encodeURIComponent("@Html.Raw(ViewData["backUrl"])&pagesize=" + curPageSize + "&pagenumber=" + curPageNumber);
        if (edit_url.indexOf('?') > 0) {
            edit_url += "&display=" + (dilplay || "0") + "&instanceid=" + (id || "") + "&@Html.Raw(ViewData["query"])";
        } else {
            edit_url += "?display=" + (dilplay || "0") + "instanceid=" + (id || "") + "&@Html.Raw(ViewData["query"])";
        }
        if ("0" == edit_model) {//当前窗口
            window.location = edit_url + "&returnurl=" + backUrl;
        } else {//弹出层
            if (!edit_width || "0" == edit_width) {
                edit_width = 800;
            }
            if (!edit_height || "0" == edit_height) {
                edit_height = 500;
            }
            openModel = edit_model;
            if (edit_model == "2") {
                openModel = 3;
            }
            top.openApp(edit_url + "&isrefresparent=1", openModel, edit_title, "@ViewData["tabid"]", edit_width, edit_height, true, "@ViewData["tabid"]", window);
            //new RoadUI.Window().open({url:edit_url, showico:false, width:edit_width, height:edit_height, openerid:tabId});
        }
        return true;
    }
    //删除数据
    function del(id, a) {
        if (!confirm('您真的要删除吗?')) {
            return false;
        }
        if (a) {
            $(a).removeAttr("onclick", "");
        }
        $.ajax({
            url: "Run_Delete?@Html.Raw(ViewData["query"])",
            data: { "delid": id || "", "__RequestVerificationToken": $("input[name='__RequestVerificationToken']").val() },
            cache: false, type: "POST", dataType: "json",
            success: function (json) {
                var msg = json.msg;
                var success = json.success;
                alert(msg + "!");
                if (1 == success) {
                    query(curPageSize, curPageNumber);
                }
                //$(but).prop("disabled", false);
            }
        });
    }
    //导出EXCEL
    function exportExcel() {
        var url = "Run_Export?@Html.Raw(ViewData["query"])";
        window.location = url;
    }
    //自定义的脚本
    @Html.Raw(Model.ClientScript)
</script>